package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Destination;
import cn.wolfcode.wolf2w.business.api.domain.Region;
import cn.wolfcode.wolf2w.business.mapper.DestinationMapper;
import cn.wolfcode.wolf2w.business.mapper.RegionMapper;
import cn.wolfcode.wolf2w.business.query.DestinationQuery;
import cn.wolfcode.wolf2w.business.service.IDestinationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 目的地Service业务层处理
 * 
 * @author yuhao
 * @date 2025-09-16
 */
@Service
@Transactional
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper,Destination> implements IDestinationService {


    @Autowired
    private RegionMapper regionMapper;



    @Override
    public IPage<Destination> queryPage(DestinationQuery qo) {
        IPage<Destination> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<Destination> queryByRegionId(Long regionId) {
        List<Destination> list=null;
        if(regionId==-1){
            //国内
             list= lambdaQuery().eq(Destination::getParentId, 1).list();

        }else {
            Region region = regionMapper.selectById(regionId);//再数据库中查询
            String refIds = region.getRefIds();  //获取关联ID
            String[] split = refIds.split(",");  //分割字符串
            //split(",") 方法会按照逗号 , 作为分隔符，将 refIds 字符串分割成多个子字符串
            //返回一个 String[] 数组，数组中的每个元素都是一个独立的ID字符串
            list=new ArrayList<>(); //初始化
            for (String string : split) {
                Long id = Long.valueOf(string);//将字符串形式的ID转换为 Long 类型
                Destination destination = baseMapper.selectById(id);
                list.add(destination);
            }
        }

        //查询二级路径
        for (Destination destination : list) {
            List<Destination> secondLevel = lambdaQuery().eq(Destination::getParentId, destination.getId())
                    .last("limit 10").list();  //取前10个
            destination.setChildren(secondLevel);
        }
        return list;
    }

    @Override
    public List<Destination> queryToasts(Long destId) {
        List<Destination> list = new ArrayList<>();
        createToast(destId,list);
        Collections.reverse(list);  //将内容反序排列
        return list;
    }

    //递归
    private void createToast(Long destId,List<Destination> list) {
        if(destId==null){
            return;
        }
        Destination destination = baseMapper.selectById(destId);
        createToast(destination.getParentId(),list);
        list.add(destination);

    }
}
